﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Boggle;
using Common.DataStructures;
using NUnit.Framework;

namespace BoggleIntegrationTests
{
    [TestFixture]
    public class BoggleSolverTests
    {
        [Test]
        public void Solve()
        {
            TestBoggleDictionary boggleDictionary = new TestBoggleDictionary("EnglishDictionary.txt");
            BoggleSolver solver = new BoggleSolver(boggleDictionary);
            BoggleBoard board = new BoggleBoard(3, 3, new string[]{"y", "o", "x", "r", "b", "a", "v", "e", "d"});
            IEnumerable<BoggleTraversal> solutions = solver.Solve(board);
            List<String> expectedSolutions = new List<string> { "abed", "aero", "aery", "bade", "bead", "bread", "bore", "bored", "bred", "broad", "byroad", "derby", "orbed", "orby", "oread", "read", "road", "robe", "robed", "verb", "very", "yore" };
            Assert.That(solutions.Select(solution => solution.ToString()), Is.EquivalentTo(expectedSolutions));
        }
    }

    public class TestBoggleDictionary : IBoggleDictionary
    {
        private readonly Trie<String> _dictionary = new Trie<String>();
        public TestBoggleDictionary(IEnumerable<string> entries)
        {
            foreach (string entry in entries)
            {
                _dictionary.AddValue(entry, entry);
            }
        }

        public TestBoggleDictionary(string dictionaryPath)
         {
             foreach (string word in File.ReadAllLines(dictionaryPath))
             {
                 _dictionary.AddValue(word, word);
             }
         }

        public SearchResult Search(string word)
        {
            return _dictionary.Search(word);
        }
    }
}
